package com.coding.kan_yi_dao.service.impl;

import com.coding.kan_yi_dao.common.Result;
import com.coding.kan_yi_dao.entity.GoodsInfo;
import com.coding.kan_yi_dao.entity.GoodsLike;
import com.coding.kan_yi_dao.mapper.GoodsInfoMapper;
import com.coding.kan_yi_dao.service.IGoodsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 砍一刀，不买就砍你一刀
 * @since 2023-09-20
 */
@Service
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements IGoodsInfoService {
    //查询
    @Override
    public Result go_select(GoodsInfo goodsInfo) {
        if (goodsInfo.getGoodsId() == null) {
            List<GoodsInfo> a = baseMapper.selectList(null);
            return Result.ok(a);
        }
        GoodsInfo a = baseMapper.selectById(goodsInfo);
        return Result.ok(a);
    }

    //查询
    @Override
    public GoodsInfo go_selects(Integer id) {
        return baseMapper.selectById(id);
    }

    //分类管理
    @Override
    public Result go_info_goods(GoodsInfo goodsInfo) {
        return Result.ok(query().eq("goods_category_id", goodsInfo.getGoodsCategoryId()).list());
    }

    //商品添加
    @Override
    public Result go_info_insert(GoodsInfo goodsInfo) {
        return Result.ok(baseMapper.insert(goodsInfo));
    }

    //修改
    @Override
    public Result go_update(GoodsInfo goodsInfo) {
        return Result.ok(baseMapper.updateById(goodsInfo));
    }

    //模糊查询
    @Override
    public Result go_select_like(String goodsname) {
        return Result.ok(query().like("goods_name", goodsname).list());
    }

    @Override
    public Result go_update_insert(List<Integer> ids) {
        List<GoodsInfo> goodsInfos = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            GoodsInfo goodsInfo = new GoodsInfo();
            goodsInfo.setGoodsId(ids.get(i));
            goodsInfo.setGoodsSellStatus(1);
            goodsInfos.add(goodsInfo);
        }
        return Result.ok(updateBatchById(goodsInfos));
    }

    @Override
    public Result go_update_delete(List<Integer> ids) {
        List<GoodsInfo> goodsInfos = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            GoodsInfo goodsInfo = new GoodsInfo();
            goodsInfo.setGoodsId(ids.get(i));
            goodsInfo.setGoodsSellStatus(0);
            goodsInfos.add(goodsInfo);
        }
        return Result.ok(updateBatchById(goodsInfos));
    }

    //

}
